layui.define(['table', 'form','selectN'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form,selectN = layui.selectN;

    selectN({
        elem: '#types'
        , name: 'types'
        , search: [true, true]
        , data: layui.setter.webRoot +'/market/cloud/goods/type/tree/select.data'
        , field: {
            idName: 'id',
            titleName: 'name',
            statusName: 'status',
            childName: 'children'
        }
    });

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/market/cloud/goods/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
      ,size:'lg'
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', width: 100, title: 'ID'}
      ,{field: 'name', title: '商品名称', minWidth: 100}
      ,{field: 'thumbnailImg', title: '商品图片', templet: '#imgTpl'}
      ,{field: 'classifyName', title: '商品分类'}
      ,{field: 'typeNames', title: '商品类型'}
      ,{field: 'supplierName', title: '供货商'}
      ,{field: 'payMoneyStr', title: '价格(元)'}
      ,{field: 'stateName',title: '状态',templet:function (record){
          if(record.state == 1) {
              return '<span class="layui-badge layui-bg-green">'+record.stateName +'</span>';
          }else if(record.state == 2) {
              return '<span class="layui-badge layui-bg-orange">'+record.stateName + '</span>';
          }else if(record.auditState == 3){
              return '<span class="layui-badge">'+record.stateName + '</span>';
          }else{
              return record.stateName;
          }
      }}
      ,{field: 'createDate', title: '创建时间', sort: true}
      ,{title: '操作', width: 150, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
    });
  });
  
  //事件
  var active = {
    info:function(obj){
    	layer.open({
            type: 2
            ,title: '查看商品详情'
            ,content: layui.setter.webRoot + '/market/cloud/goods/info.html?id='+obj.data.id
            ,maxmin: true
            ,area: ['90%', '90%']
          });
    }
    ,del:function(obj){
    	layer.confirm('确定要删除该商品吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/market/cloud/goods/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
  };
  
  $('.layui-btn.layui-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('cloud_goods_list', {})
});